<template>
  <div class="app-container">
    <!-- 添加或修改项目基础信息管理对话框 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="产品编码" prop="goodsCode">
        <el-input v-model="queryParams.goodsCode" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品型号" prop="goodModel">
        <el-input v-model="queryParams.goodModel" placeholder="请输入产品型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--<el-form-item label="需求日期" prop="saleDate">
        <el-date-picker clearable v-model="queryParams.params['saleDate']" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择下单日期">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item label="需求日期">
        <el-date-picker v-model="dateRange"
                        style="display: flex;"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="是否拆解" prop="ischaijie">
        <el-select v-model="queryParams.ischaijie" placeholder="是否拆解" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.ischaijie" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="是否自制" prop="isdo">
        <el-select v-model="queryParams.isdo" placeholder="是否自制" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.isdo" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>-->
      <el-form-item label="工序分配状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="工序分配状态" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.sale_state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-plus" type="success" size="mini" @click="addBnt">新增产品</el-button>
        <el-popconfirm title="确认删除选择的数据项？" @confirm="handlerukuAll()" style="margin-left: 20px;">
          <el-button slot="reference" type="danger" plain icon="el-icon-close" size="mini" >批量删除
          </el-button>
        </el-popconfirm>
        <!--<el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExportDetail" >导出
        </el-button>-->
      </el-form-item>
    </el-form>
    <!--<el-table :data="dataList" :max-height="tableMaxHeight" @selection-change="handleSelectionChange"
              v-loading="loading">-->
    <el-col :span="1.5">
      <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="multiple"
        @click="piliangFenpeiGx">批量分配工艺工序</el-button>
    </el-col>
    <el-table :data="columns" ref="dragTable" row-key="id" v-loading="loading" @selection-change="handleSelectionChange"  @sort-change="handleSortChange"
      :max-height="tableMaxHeight">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="序号" type="index" width="50">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="产品编码" align="center" prop="goodsCode"/> -->
      <el-table-column sortable label="产品编码" align="center" prop="goodsCode">
        <template slot-scope="scope">
            <span>{{ scope.row.goodsCode }}</span>
        </template>
      </el-table-column>

      <!--<el-table-column sortable label="产品状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_status" :value="scope.row.status" />
        </template>
      </el-table-column>-->
      <el-table-column sortable align="center" label="工序分配状态" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sale_state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column sortable align="center" label="产品名称" prop="goodsName"  show-overflow-tooltip/>
      <el-table-column sortable align="center" label="产品型号" prop="goodModel" />
      <!--<el-table-column sortable align="center" label="产品单位" prop="unit" />-->
      <el-table-column align="center" label="订单数量" prop="orderCount" />
      <el-table-column sortable label="需求日期" align="center" prop="deliveryTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否自制" align="center" prop="isdo">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.isdo" :value="scope.row.isdo" />
        </template>
      </el-table-column>
      <el-table-column label="是否拆解" align="center" key="ischaijie" prop="ischaijie">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ischaijie" :value="scope.row.ischaijie"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="自制数量" prop="pleaseBuyCount" />
      <el-table-column align="center" label="巡检单" prop="xunjianFile" width="200">
        <template slot-scope="scope" v-if="scope.row.xunjianFile">
          <image-preview :src="scope.row.xunjianFile" height="60px" width="40px"></image-preview>
        </template>
      </el-table-column>
      <el-table-column align="center" label="备注" prop="remark" />
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button @click="choseGy(scope.row)" v-if="scope.row.isdo==1 && scope.row.state!=2" icon="el-icon-view"
            size="mini" type="text">
            设定工序
          </el-button>
          <el-button @click="lookGy(scope.row)" v-if="scope.row.isdo==1 && scope.row.state!=2" icon="el-icon-view"
                     size="mini" type="text">
            工序查看
          </el-button>
          <el-button @click="chaijie(scope.row)" icon="el-icon-view" size="mini" type="text">
            串行拆解
          </el-button>
          <el-button @click="addBnt(scope.row)" icon="el-icon-view" size="mini" type="text">
            修改
          </el-button>
          <el-popconfirm title="确认删除选择的数据项？" @confirm="handlerukubnt(scope.row)" style="margin-left: 20px;">
            <el-button slot="reference" type="text" icon="el-icon-close" size="mini" >删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <choseGx-form @ok="getList" ref="choseGxForm"></choseGx-form>
    <piliangFenpeiGxp-form @ok="getList" ref="piliangFenpeiGxpForm"></piliangFenpeiGxp-form>
    <chajie-form @ok="getList" ref="chaijieForm"></chajie-form>
    <fpTaskForm-list @ok="getList" ref="taskFpForm"></fpTaskForm-list>
    <xunjian-form @ok="getList" ref="xunjianForm"></xunjian-form>
    <addGoods-form  @ok="getList" ref="addgoodsform"></addGoods-form>
    <goodsSaleGongxu-form @ok="getList" ref="goodsSaleGongxuForm"></goodsSaleGongxu-form>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
  import {
    addAction,
    deleteAction,
    downloadFile,
    getAction,
    getById
  } from '@/api/manage'
  import AddGoodsForm from "@/views/develop/projectBaseInfo/module/AddGoodsForm";
  import ChoseGxForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ChoseGxForm";
  import PiliangFenpeiGxpForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/PiliangFenpeiGxpForm";
  import ChajieForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ChajieForm";
  import FpTaskFormList from "@/views/develop/projectBaseInfo/prjForGoodsIn/FpTaskFormList";
  import XunjianForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/XunjianForm";
  import GoodsSaleGongxuForm from "@/views/develop/goodsSaledoGongxu/module/GoodsSaleGongxuForm";
  import { listSaleNotProject } from "@/api/develop/goodsSale";

  export default {
    name: "FenpeiGxList",
    dicts: ['isdo', 'sale_state', 'goods_status','ischaijie'],
    components: {
      ChoseGxForm,
      PiliangFenpeiGxpForm,
      ChajieForm,
      FpTaskFormList,
      XunjianForm,
      GoodsSaleGongxuForm,
      AddGoodsForm
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        activeName: "first",
        // 弹出层标题
        title: "设定产品工序",
        // 是否显示弹出层
        open: false,
        single: true,
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 表单参数
        form: {},
        proId: "",
        dateRange: [],
        proStatus: "",
        columns: [],
        total: 0,
        ids: [],
        goodsIds: [],
        // 表单校验
        rules: {},
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          params: {},
          id: null,
          code: null,
          projectNo: null,
          gongchengName: null,
          goodsName: null,
          goodsCode: null,
          goodModel: null,
          model: null,
          brand: null,
          //isdo: "1",//自制的
          remark: null,
          projectId: null,
          ischaijie: null,
          orderByColumn: "create_time",
          isAsc: "desc"
        },
        queryParams2: {
          pageNum: 1,
          pageSize: 10000,
          params: {},
          id: null,
        },
        url: {
          list: '/goodsSale/goodsSale/selectListNotProject',
          getById: '/projectBaseInfo/projectBaseInfo/',
          remove: '/goodsSale/goodsSale/',
        },
        tableMaxHeight: window.innerHeight - 320,
      };
    },
    created() {
      //this.handleQuery();
      this.getList();
      window.addEventListener('resize', this.getTableHeight);
    },
    methods: {
      handlerukubnt(e) {
        deleteAction(this.url.remove, e.id).then(response => {
          this.getList();
          });
      },
      handlerukuAll() {
        deleteAction(this.url.remove, this.ids).then(response => {
          this.getList();
          });
      },
      lookGy(row){
        this.$refs.goodsSaleGongxuForm.xunjianShow(row);
      },
        /** 排序触发事件 */
     handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
      xunjian(row) {
        this.$refs.xunjianForm.xunjianShow(row);
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.goodsIds = selection.map(item => item.goodsId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      piliangFenpeiGx() {
        // 检查 this.goodsIds 中是否都一样，不一样进行提示
        if (this.goodsIds.length > 1) {
          const firstGoodsId = this.goodsIds[0];
          const allSame = this.goodsIds.every(goodsId => goodsId === firstGoodsId);
          if (!allSame) {
            this.$message.warning('选择的产品编码不一致，请重新选择');
          } else {
            this.$refs.piliangFenpeiGxpForm.piliangFenpeiGxpShow(this.ids, this.goodsIds);
          }
        } else {
          this.$refs.piliangFenpeiGxpForm.piliangFenpeiGxpShow(this.ids, this.goodsIds);
        }

      },
      taskFp(row) {
        this.$refs.taskFpForm.taskFpShow(row);
      },
      chaijie(row) {
        this.$refs.chaijieForm.chaijieGoods(row);
      },
      choseGy(row) {
        this.$refs.choseGxForm.choseGy(row);
      },

      getList() {
        this.loading = true;
        /*getAction(this.url.list, this.queryParams).then(response => {
          this.columns = response.rows;
          this.total = response.total;
          this.loading = false;
        });*/
        listSaleNotProject(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.columns = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.queryParams.params.salesmanName = ''
        this.queryParams.params.orderDate = ''
        this.resetForm("queryForm");
        this.handleQuery();
      },
      addBnt(e) {
        this.$refs.addgoodsform.show(e)
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      reset() {
        this.form = {
          id: null,
          remark: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },

      handleExportDetail(row) {
        downloadFile(this.url.exportData, this.queryParams);
      },

    }
  };
</script>
